﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Diagnostics;
    using System.Drawing.Design;
    using System.Text;
    using System.Web;
    using System.Web.UI.Design;
    using System.Windows.Forms;

    public class GridActionList : DesignerActionList
    {
        private string _searchText;
        private DesignerActionUIService desginerActionUISvc;
        private GridDesigner designer;
        private Vdc.Grid.Grid GridComponent;
        private int highlightedColumnIndex;

        public GridActionList(GridDesigner designer, int highlightedColumnIndex) : base(designer.Component)
        {
            this.highlightedColumnIndex = -1;
            this.GridComponent = designer.Component as Vdc.Grid.Grid;
            this.designer = designer;
            this.desginerActionUISvc = base.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
            this.highlightedColumnIndex = highlightedColumnIndex;
        }

        private void AddColumn()
        {
            MessageBox.Show("Not implemented yet.");
        }

        private PropertyDescriptor GetPropertyByName(string propName)
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GridComponent)[propName];
            if (descriptor == null)
            {
                throw new ArgumentException("Matching Grid property not found!", propName);
            }
            return descriptor;
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            items.Add(new DesignerActionHeaderItem("Columns"));
            items.Add(new DesignerActionHeaderItem("Appearance"));
            items.Add(new DesignerActionHeaderItem("General Settings"));
            items.Add(new DesignerActionHeaderItem("Paging/Sorting"));
            items.Add(new DesignerActionHeaderItem("Online Resources"));
            items.Add(new DesignerActionHeaderItem("Templates"));
            items.Add(new DesignerActionPropertyItem("AutoGenerateColumns", "Auto-Generate Columns", "Columns", ""));
            if (!this.AutoGenerateColumns)
            {
                items.Add(new DesignerActionMethodItem(this, "LaunchColumnsCollectionEditor", "Edit Columns...", "Columns", true));
                if (this.highlightedColumnIndex != -1)
                {
                    if (this.highlightedColumnIndex > 0)
                    {
                        items.Add(new DesignerActionMethodItem(this, "MoveColumnLeft", "Move Column Left", "Columns", true));
                    }
                    if (this.highlightedColumnIndex < (this.GridComponent.Columns.Count - 1))
                    {
                        items.Add(new DesignerActionMethodItem(this, "MoveColumnRight", "Move Column Right", "Columns", true));
                    }
                    items.Add(new DesignerActionMethodItem(this, "RemoveColumn", "Remove Column", "Columns", true));
                }
            }
            items.Add(new DesignerActionPropertyItem("FolderStyle", "Folder with Styles", "Appearance", ""));
            items.Add(new DesignerActionPropertyItem("CallbackMode", "Callback Mode", "General Settings", ""));
            items.Add(new DesignerActionPropertyItem("Serialize", "Serialize DataSource into ViewState", "General Settings", ""));
            items.Add(new DesignerActionPropertyItem("AllowRecordSelection", "Allow Selection of Records", "General Settings", ""));
            items.Add(new DesignerActionPropertyItem("AllowAddingRecords", "Allow Adding Records", "General Settings", ""));
            items.Add(new DesignerActionPropertyItem("AllowFiltering", "Allow Filtering", "General Settings", ""));
            items.Add(new DesignerActionPropertyItem("AllowGrouping", "Allow Grouping", "General Settings", ""));
            items.Add(new DesignerActionPropertyItem("AllowSorting", "Allow Sorting", "Paging/Sorting", ""));
            items.Add(new DesignerActionPropertyItem("AllowPaging", "Allow Paging", "Paging/Sorting", ""));
            items.Add(new DesignerActionPropertyItem("AllowPageSizeSelection", "Allow PageSize Selection", "Paging/Sorting", ""));
            items.Add(new DesignerActionMethodItem(this, "NavigateToGridWebsite", "Vdc Home Page", "Online Resources", true));
            items.Add(new DesignerActionMethodItem(this, "NavigateToGridHowTo", "Vdc How-To and Help", "Online Resources", true));
            items.Add(new DesignerActionMethodItem(this, "NavigateToGridKB", "Vdc Knowledge Base", "Online Resources", true));
            items.Add(new DesignerActionHeaderItem("Search vdc.com.vn for:", "Online Resources"));
            items.Add(new DesignerActionPropertyItem("SearchText", "", "Online Resources", ""));
            items.Add(new DesignerActionMethodItem(this, "SearchOboutCom", "Search", "Online Resources", true));
            items.Add(new DesignerActionMethodItem(this, "LaunchTemplatesCollectionEditor", "Manage Templates", "Templates", true));
            return items;
        }

        private void LaunchColumnsCollectionEditor()
        {
            EditorServiceContext.EditValue(this.designer, base.Component, "Columns");
        }

        private void LaunchTemplatesCollectionEditor()
        {
            EditorServiceContext.EditValue(this.designer, base.Component, "Templates");
        }

        private void MoveColumnLeft()
        {
            Cursor current = Cursor.Current;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                ControlDesigner.InvokeTransactedChange(this.GridComponent, new TransactedChangeCallback(this.MoveColumnLeftCallback), null, "MoveColumnLeft");
            }
            finally
            {
                Cursor.Current = current;
            }
        }

        protected bool MoveColumnLeftCallback(object context)
        {
            if (this.highlightedColumnIndex > 0)
            {
                Column columnByIndex = this.GridComponent.Columns.GetColumnByIndex(this.highlightedColumnIndex);
                Column column2 = this.GridComponent.Columns.GetColumnByIndex(this.highlightedColumnIndex - 1);
                int index = column2.Index;
                column2.Index = columnByIndex.Index;
                columnByIndex.Index = index;
                this.designer.HighlightColumn(index);
                return true;
            }
            return false;
        }

        private void MoveColumnRight()
        {
            Cursor current = Cursor.Current;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                ControlDesigner.InvokeTransactedChange(this.GridComponent, new TransactedChangeCallback(this.MoveColumnRightCallback), null, "MoveColumnRight");
            }
            finally
            {
                Cursor.Current = current;
            }
        }

        protected bool MoveColumnRightCallback(object context)
        {
            if (this.highlightedColumnIndex < (this.GridComponent.Columns.Count - 1))
            {
                Column columnByIndex = this.GridComponent.Columns.GetColumnByIndex(this.highlightedColumnIndex);
                Column column2 = this.GridComponent.Columns.GetColumnByIndex(this.highlightedColumnIndex + 1);
                int index = column2.Index;
                column2.Index = columnByIndex.Index;
                columnByIndex.Index = index;
                this.designer.HighlightColumn(index);
                return true;
            }
            return false;
        }

        private void NavigateToGridHowTo()
        {
            Process.Start("http://www.vdc.com.vn/");
        }

        private void NavigateToGridKB()
        {
            Process.Start("http://www.vdc.com.vn/");
        }

        private void NavigateToGridWebsite()
        {
            Process.Start("http://www.vdc.com.vn/");
        }

        private void RemoveColumn()
        {
            Cursor current = Cursor.Current;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                ControlDesigner.InvokeTransactedChange(this.GridComponent, new TransactedChangeCallback(this.RemoveColumnCallback), null, "RemoveColumn");
            }
            finally
            {
                Cursor.Current = current;
            }
        }

        protected bool RemoveColumnCallback(object context)
        {
            if (this.highlightedColumnIndex != -1)
            {
                Column columnByIndex = this.GridComponent.Columns.GetColumnByIndex(this.highlightedColumnIndex);
                this.GridComponent.Columns.Remove(columnByIndex);
                return true;
            }
            return false;
        }

        private void SearchOboutCom()
        {
            Process.Start("http://www.vdc.com.vn/Search.aspx?key=" + HttpUtility.UrlDecode(this._searchText, Encoding.Default));
        }

        public bool AllowAddingRecords
        {
            get
            {
                return this.GridComponent.AllowAddingRecords;
            }
            set
            {
                this.GetPropertyByName("AllowAddingRecords").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool AllowFiltering
        {
            get
            {
                return this.GridComponent.AllowFiltering;
            }
            set
            {
                this.GetPropertyByName("AllowFiltering").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool AllowGrouping
        {
            get
            {
                return this.GridComponent.AllowGrouping;
            }
            set
            {
                this.GetPropertyByName("AllowGrouping").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool AllowPageSizeSelection
        {
            get
            {
                return this.GridComponent.AllowPageSizeSelection;
            }
            set
            {
                this.GetPropertyByName("AllowPageSizeSelection").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool AllowPaging
        {
            get
            {
                return this.GridComponent.AllowPaging;
            }
            set
            {
                this.GetPropertyByName("AllowPaging").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool AllowRecordSelection
        {
            get
            {
                return this.GridComponent.AllowRecordSelection;
            }
            set
            {
                this.GetPropertyByName("AllowRecordSelection").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool AllowSorting
        {
            get
            {
                return this.GridComponent.AllowSorting;
            }
            set
            {
                this.GetPropertyByName("AllowSorting").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool AutoGenerateColumns
        {
            get
            {
                return this.GridComponent.AutoGenerateColumns;
            }
            set
            {
                this.GetPropertyByName("AutoGenerateColumns").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public bool CallbackMode
        {
            get
            {
                return this.GridComponent.CallbackMode;
            }
            set
            {
                this.GetPropertyByName("CallbackMode").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        [Editor(typeof(GridSpecialFolderEditor), typeof(UITypeEditor))]
        public string FolderStyle
        {
            get
            {
                return this.GridComponent.FolderStyle;
            }
            set
            {
                this.GetPropertyByName("FolderStyle").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }

        public string SearchText
        {
            get
            {
                return this._searchText;
            }
            set
            {
                this._searchText = value;
            }
        }

        public bool Serialize
        {
            get
            {
                return this.GridComponent.Serialize;
            }
            set
            {
                this.GetPropertyByName("Serialize").SetValue(this.GridComponent, value);
                this.desginerActionUISvc.Refresh(base.Component);
            }
        }
    }
}

